﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

namespace LiujunExcelFunc
{
    [Guid("7916ECF3-3CD1-4e28-876C-4D9D8A5EE8FA")]
    [ClassInterface(ClassInterfaceType.AutoDual), ComVisible(true)]    
    public class MyFunc
    {
        #region
        [ComRegisterFunction]
        public static void RegisterFunction(Type type)
        {
            Registry.ClassesRoot.CreateSubKey(GetSubKeyName(type, "Programmable"));
            var key = Registry.ClassesRoot.OpenSubKey(GetSubKeyName(type, "InprocServer32"), true);
            key.SetValue("", Environment.SystemDirectory + @"\mscoree.dll", RegistryValueKind.String);

        }

        [ComUnregisterFunction]
        public static void UnregisterFunction(Type type)
        {
            Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(type, "Programmable"), false);
        }

        private static string GetSubKeyName(Type type, string subKeyName)
        {
            var s = new System.Text.StringBuilder();
            s.Append(@"CLSID\{");
            s.Append(type.GUID.ToString().ToUpper());
            s.Append(@"}\");
            s.Append(subKeyName);
            return s.ToString();
        }
        #endregion 
        /*---------------------------------------------------------自定义EXCEL函数---------------------------------------------------------*/
        /// <summary>
        /// 通过正则表达式匹配，返回第一个匹配的字符串
        /// </summary>
        /// <param name="Source">源字符串</param>
        /// <param name="RegString">正则表达式</param>
        /// <returns></returns>
        public String MyMatch(String Source, String RegString)
        {
            String Regtemp = @"";
            Regtemp = Regtemp + RegString;
            Match match = Regex.Match(Source, Regtemp);
            if (match.Success)
            {
                return match.Value;
            }
            else
                return "";
        }

        /// <summary>
        /// 通过正则表达式匹配，返回全部匹配的字符串
        /// </summary>
        /// <param name="Source">源字符串</param>
        /// <param name="RegString">正则表达式</param>
        /// <returns></returns>
        public String MyMatches(String Source, String RegString)
        {
            String Regtemp = @"";
            Regtemp = Regtemp + RegString;

            MatchCollection matches = Regex.Matches(Source, Regtemp);
            if (matches.Count > 0)
            {
                String strTemp = matches[0].Value;
                for (int i = 1; i < matches.Count; i++)
                {
                    strTemp = strTemp + "," + matches[i].Value;
                }
                return strTemp;
            }
            else
                return "";
            
        }



        /// <summary>
        /// 通过正则表达式匹配，以指定字符串替换匹配正则表达式的字符串
        /// </summary>
        /// <param name="Source"></param>
        /// <param name="RegString"></param>
        /// <param name="Replace"></param>
        /// <returns></returns>
        public String MyReplace(String Source, String RegString,String Replace)
        {
            String Regtemp = @"";
            Regtemp = Regtemp + RegString;

            string resultString = null;
            try
            {
                resultString = Regex.Replace(Source, RegString, Replace);
            }
            catch (ArgumentException ex)
            {
                // Syntax error in the regular expression
            }

            return resultString;
        }

        /// <summary>
        /// 重载了MyReplace方法，以空字符串替换匹配正则表达式的字符串
        /// </summary>
        /// <param name="Source">源字符串</param>
        /// <param name="RegString">正则表达式</param>
        /// <returns></returns>
        public String MyReplace(String Source, String RegString)
        {
            return MyReplace(Source, RegString, "");
        }		

    }
}
